package edu.westga.vocalengineer.audio.transforms;

/**
 * Applies a simple echo transform to a sound, provided as a short buffer.
 * @author mfarmer2
 *
 */
public class EchoTransform implements ITransform {
	
	protected int echoDelay;
	protected float echoStrength;
	
	/**
	 * Sets up a simple echo transform.
	 * @param delay  The delay in samples.  
	 * @param strength  The strength of the echo as a ratio (i.e. 1.0 = 100%)
	 */
	public EchoTransform(int delay, float strength) {
		echoDelay = delay;
		echoStrength = strength;
	}
	
	/**
	 * Executes the echo on a buffer, actually applying it to the data.
	 */
	@Override
	public short[] execute(short[] samples) {
		
		short[] filtered = new short[samples.length + echoDelay];
		
		// first take care of the unmodified part before the first echo 
		for(int i = 0; (i < echoDelay); i++) {
			filtered[i] = samples[i];
		}
		
		// do it to it
		for(int i = echoDelay; i < samples.length; i++) {
			filtered[i] =  
				(short) (samples[i] + (short)((float)samples[i - echoDelay] * echoStrength));
		}	
		
		// handle trailing echo
		for(int i = samples.length; i < filtered.length; i++) {
			filtered[i] = (short)((float)samples[i-echoDelay] * echoStrength);
		}
		
		return filtered;
	}

}
